home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: Empty String function
- Date: 19 Mar 1996 11:00:25 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4imlmp$j4q@umbc9.umbc.edu>
- References: <1996Mar14.205741.19178@vtf.idx.com>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Steve Mount <sjjm@hawkeye.idx.com> wrote:
- |> I benchmarked several methods for testing a buffer's
- |> emptiness. Empty is defined as all characters being space,
- |> newline, or tab. I hit on one very fast method; it
- |> blew the others out of the water. I presume the standard
- |> function I'm using is optimized. But it has a potential
- |> problem I'm concerned about - can anyone tell me if it
- |> could be illegal, or at the least, bad form?
- |> Here it is:
- |>
- |> int zemp(char *ptxt, int len)
- |> {
- |> static int i;
- |>
- |> if (len == 0) len = strlen(ptxt); // Allow null-term strings
- |> i = strspn(ptxt," \n\t");
- |> if (i >= len) return(-1); // -1 == EMPTY
- |> return(i);
- |> }
-
- Well you have used C++ comments which are not supported by ANSI C compilers
- which are compliant with the standard. Also defining 'i' as static doesn't
- seem to do much except cause sizeof(int) to be permanently allocated
- to the zemp() function. I guess if you call this function a lot then
- maybe it could help.
-
- |> The problem is when the buffer is not null-terminated, which
- |> happens a lot. The strspn function COULD read past the end
- |> of the buffer.... potentially WAY past it. Possibly even into
- |> memory not owned by the program. This is where I get concerned.
-
- I would be concerned as well. The C library <string.h> is written with
- NUL (not null as you wrote) terminated strings in mind. From your code
- we cannot even be sure if you #included this. If you desire some other
- definition of a string you will need to write your own functions from
- scratch.
-
- |> Anyone know if this could cause trouble, generally, and if
- |> it could cause trouble specifically in DOS/Win/Unix (it is
- |> used in a Unix app now).
-
- Accessing memory past an array's upper limit is illegal on any platform.
- Some compilers may be smarter at detecting this at compile time, but
- none are required to.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-